<?php

namespace Admin\Controller;

use Think\Controller;
use Org\Net\Http;

class ScenicController extends Controller {
	public function index() {
		$http = new Http ();
		$geotable_id = 111203;
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$total = $back ['total'];
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}&page_size={$total}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$list = $back ['pois'];
		$this->assign ( 'list', $list );
		$this->display ();
	}
// 	public function index(){
// 		$scenic = D("Scenic");
// 		$list = $scenic->select();
// 		for($i=0;$i<count($list);$i++){
// 			$list[$i]['image'] = split(",",$list[$i]['image']);
// 		}
// 		$this->assign ('list', $list );
// 		$this->display ();
// 	}
	//百度地图转本地数据库
	public function change() {
		$http = new Http ();
		$geotable_id = 111203;
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$total = $back ['total'];
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}&page_size={$total}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$list = $back ['pois'];
		$this->assign ( 'list', $list );
		$baidu = D("Baidu");
		foreach ( $list as $poi ) {
			$arr = array ();
			$arr ['id'] = $poi ['id'];
			$arr ['title'] = $poi ['title'];
			$arr ['city'] = $poi ['city'];
			$arr ['province'] = $poi ['province'];
			$arr ['district'] = $poi ['district'];
			$arr ['address'] = $poi ['address'];
			$arr ['longitude'] = $poi ['location'] [0];
			$arr ['latitude'] = $poi ['location'] [1];
			$baidu->add ( $arr );
		}
	}
	//给本地景区表添加地区以及
	public function test(){
		$http = new Http ();
		$geotable_id = 111203;
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$total = $back ['total'];
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}&page_size={$total}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$list = $back ['pois'];
		$this->assign ( 'list', $list );
		foreach ( $list as $poi ) {
			$province = D("Province");
			$p = $province->where(array('province'=>$poi ['province']))->find();
// 			if(!$p){
// 				$province->add(array('province'=>$poi ['province']));
// 			}
			$city = D("City");
			$c = $city->where(array('city'=>$poi ['city']))->find();
			if(!$c){
				$city->add(array('city'=>$poi ['city'],'province_id'=>$p['id']));
			}
		}
	}
	public function add() {
		
	}
	public function delete() {
		// $where ['id'] = I ( 'id' );
		$http = new Http ();
		$post ['geotable_id'] = '111203';
		$post ['id'] = I ( 'id' );
		$post ['ak'] = C ( "BAIDU_AK" );
		$data ['post'] = $post;
		$back = $http->fsockopenDownload ( C ( "BAIDU_POI_DELETE" ), $data );
		// file_put_contents("Public/Log/log.txt", "back:".$back."\r\n",FILE_APPEND|LOCK_EX);
		$back = json_decode ( $back, 'json' );
		if ($back ['status'] == 0) {
			$this->success ( "删除成功", U ( 'Scenic/index' ) );
		} else {
			$this->error ( "删除失败" );
		}
	}
}